﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.Xml;
using System.Diagnostics;


namespace GameWP
{
    public class DBSaveGame : Database
    {
        private string _saveFileName = "OPDsave.xml";
        public SaveGameData _dataSave;
        public SaveGameData _dataFactory = new SaveGameData() { _hp = 2000, _mana = 100, _dam = 10, _level = 1, _balance = 999500, _levelScreen = 10, _levelSkill = 1, _AHeroHave = new List<int>()};

#region Initilation
        public DBSaveGame()
        {
            _dataSave = new SaveGameData();
            _dataSave = LoadGame();
            // Nghia la chua co du lieu duoc luu xuong
            if (_dataSave._hp == 0)
                _dataSave = _dataFactory;
        }
#endregion

#region Methods
        public void SaveGame(SaveGameData _saveGameData)
        {
            XmlWriterSettings xmlWriterSetting = new XmlWriterSettings();
            xmlWriterSetting.Indent = true;
              // Lấy thông tin IsolateStorage
              using (IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
              {
                  // Tạo một FileStream để tạo file hay mở file
                  using(IsolatedStorageFileStream FileStream = new IsolatedStorageFileStream(_saveFileName, System.IO.FileMode.Create, isoStorage))
                  {
                      XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
                      using (XmlWriter xmlWriter = XmlWriter.Create(FileStream,xmlWriterSetting))
                      {
                          // Viết dữ liệu theo Serialize
                          serializer.Serialize(xmlWriter,_saveGameData);
                      }
                  }
              }
        }

        public SaveGameData LoadGame()
        {
            SaveGameData _data = new SaveGameData();
            _data._AHeroHave = new List<int>();
              using (IsolatedStorageFile File = IsolatedStorageFile.GetUserStoreForApplication())
              {
                  try                  
                  {
                      using (IsolatedStorageFileStream FileStream = new IsolatedStorageFileStream(_saveFileName, System.IO.FileMode.Open, File))
                      {
                          // Trong FileStream thực hiện Deserialize
                          XmlSerializer serialize = new XmlSerializer(typeof(SaveGameData));
                          // Ép kiểu về đúng kiểu ban đầu và Gán lại cho Source 
                          _data = (SaveGameData)serialize.Deserialize(FileStream);
                      };
                  }
                  catch (System.Exception ex)                  {                  }
                  
              };
            return _data;
        }
#endregion
    }
}
